
require "framework/BaseView"
require "Exm/Ball"

local TickManager = CS.PengLuaKit.TickManager;
local Vector3 = CS.UnityEngine.Vector3;
-- ball view
BallView = BaseView:New( {mViewName = "BallView"} );
BallView.mCountBall = 0;

local fRotateTickRate = 1.6;

function BallView:InitView()
	-- todo
	self.txtCountBallTransform = self.transform:Find("txtCountBall");
	self.txtCountBall = self.txtCountBallTransform:GetComponent("Text");

	self.arrayBall = self.arrayBall or {};

	local tickManager = TickManager.Instance();
	tickManager:AddFrameTickListener(self.TickHandler, false);
end

function BallView:AddBall()
	-- refresh view
	self.mCountBall = self.mCountBall + 1;
	-- self.txtCountBall.text = string.format("Add Ball Count:%d", self.mCountBall);
	-- print("BallView:AddBall: count=" .. self.txtCountBall.text);

	-- add a new ball
	local ball = Ball:New({});
	ball:Draw(self.transform);

	self.arrayBall[#self.arrayBall+1] = ball;
end

function BallView:TickHandler(delta)
	-- body
	for key, value in ipairs(BallView.arrayBall) do
		value.mBall.transform:Rotate(Vector3.forward * fRotateTickRate);
	end
end

function BallView:RefreshTextView(num)
	-- body
	self.txtCountBall.text = string.format("Add Ball Count:%d", num);
end

function BallView:Dispose()
	-- body
	-- remove event listener
	local tickManager = TickManager.Instance();
	tickManager:RemoveFrameTickListener(self.TickHandler);

	self.txtCountBall.text = "";
	for k=0, self.transform.childCount-1, 1 do
		local itemGo = self.transform:GetChild(k).gameObject;
		GameObject.Destroy(itemGo);
	end
end